home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 52 / Amiga Format AFCD52 (Issue 136, May 2000).iso / -in_the_mag- / multitasking / coders / setf / setf.opendev.e < prev    next >
Text File  |  2000-03-05  |  2KB  |  96 lines

  1. /* setf.OpenDev.e -- a program to monitor calls to OpenDevice() */
  2.  
  3. OPT OSVERSION=37
  4.  
  5. MODULE 'dos/dos', 'exec/ports', 'exec/tasks', 'exec/nodes', 'exec/memory'
  6.  
  7. CONST OFFSET=$fe44  /* execbase offset of OpenDevice() */
  8.  
  9. OBJECT mymsg
  10.   msg:mn
  11.   s, t
  12. ENDOBJECT
  13.  
  14. DEF port:PTR TO mp
  15.  
  16. PROC main()
  17.   DEF ps, us, loop, sig, oldf
  18.   IF port:=CreateMsgPort()
  19.     Forbid()     /* Don't let anyone mess things up... */
  20.     IF oldf:=SetFunction(execbase, OFFSET, {newf})
  21.       PutLong({patch}, oldf)
  22.       Permit()    /* Now we can let everyone else back in */
  23.       LEA store(PC), A0
  24.       MOVE.L A4, (A0)    /* Store the A4 register... */
  25.       ps:=Shl(1,port.sigbit)   /* Set up port and user signal bits */
  26.       us:=SIGBREAKF_CTRL_C
  27.       loop:=TRUE
  28.       WHILE loop
  29.         sig:=Wait(ps OR us)
  30.         IF sig AND ps
  31.           printmsgs()
  32.         ENDIF
  33.         IF sig AND us
  34.           loop:=FALSE
  35.         ENDIF
  36.       ENDWHILE
  37.       Forbid()   /* Paranoid... */
  38.       SetFunction(execbase, OFFSET, oldf)
  39.     ENDIF
  40.     Permit()
  41.     printmsgs()   /* Make sure the port is empty */
  42.     DeleteMsgPort(port)
  43.   ENDIF
  44. ENDPROC
  45.  
  46. /* Nicely (?) print the messages out... */
  47. PROC printmsgs()
  48.   DEF msg:PTR TO mymsg
  49.   WHILE msg:=GetMsg(port)
  50.     WriteF('Task \l\s[25] wants \r\s[20]\n',
  51.            IF msg.t THEN msg.t ELSE '*unnamed*',
  52.            IF msg.s THEN msg.s ELSE '*unnamed device*')
  53.     ReplyMsg(msg)
  54.     DisposeLink(msg.s)
  55.     DisposeLink(msg.t)
  56.     Dispose(msg)
  57.   ENDWHILE
  58. ENDPROC
  59.  
  60. /* Send a message to the patching process */
  61. PROC sendmsg()
  62.   DEF m:PTR TO mymsg, s, tsk:tc, l:ln
  63.   MOVE.L A1, s
  64.   /* Allocate a new message */
  65.   m:=New(SIZEOF mymsg)
  66.   IF s
  67.     m.s:=String(StrLen(s))
  68.     StrCopy(m.s,s,ALL)
  69.   ENDIF
  70.   tsk:=FindTask(NIL)   /* Find out who we are */
  71.   m.t:=NIL
  72.   IF tsk
  73.     l:=tsk.ln
  74.     IF l AND l.name
  75.       m.t:=String(StrLen(l.name))
  76.       StrCopy(m.t, l.name, ALL)
  77.     ENDIF
  78.   ENDIF
  79.   PutMsg(port, m)
  80. ENDPROC
  81.  
  82. /* Place to store A4 register */
  83. store:  LONG 0
  84. /* Place to store real call */
  85. patch:  LONG 0
  86.  
  87. /* The new routine which will replace the original library function */
  88. newf:
  89.   MOVEM.L D0-D7/A0-A6, -(A7)
  90.   LEA store(PC), A0
  91.   MOVE.L (A0), A4 /* Reinstate the A4 register so we can use E code */
  92.   sendmsg()
  93.   MOVEM.L (A7)+, D0-D7/A0-A6
  94.   MOVE.L patch(PC), -(A7)
  95.   RTS
  96.